/*=========================================================================
| KCalDAV
|--------------------------------------------------------------------------
| (c) 2009  Kumaran Santhanam
|
| This project is released under the GNU General Public License.
| Please see the file COPYING for more details.
|--------------------------------------------------------------------------
| Remote calendar writing class.
 ========================================================================*/

/*=========================================================================
| INCLUDES
 ========================================================================*/

#include "writer.h"
#include <kdebug.h>

/*=========================================================================
| DEFINES
 ========================================================================*/
// Use caldav_modify_object() function.
//
// If it's not set, caldav_delete_object() + caldav_add_object() is
// used for modifying objects.  This sequence is dangerous because it
// can create a race condition which results in random deletion of
// events.
//
// SOGo server returns an error on caldav_modify_object.  Use of the
// modify function will render this client unusable with SOGo.  Due to
// the above warning, it is best to wait until SOGo has modify
// support.
#define USE_CALDAV_MODIFY


/*=========================================================================
| NAMESPACE
 ========================================================================*/
using namespace KCal;


/*=========================================================================
| FUNCTIONS
 ========================================================================*/
static QByteArray encode (QTextCodec *codec, const QString &text) {
    QTextCodec::ConverterState state(QTextCodec::IgnoreHeader);
    return codec->fromUnicode(text.data(), text.length(), &state);
}


/*=========================================================================
| METHODS
 ========================================================================*/
void CalDavWriter::cleanJob() {
    CalDavJob::cleanJob();
}

int CalDavWriter::runJob(runtime_info* RT) {
    kDebug() << "writer::run, url: " << url() << "\n";

    if (mAdded.isEmpty() &&
        mChanged.isEmpty() &&
        mDeleted.isEmpty())
    {
        // nothing to do
        return OK;
    }

    int res = OK;

    if (mEncoding == NULL) {
        kDebug() << "get locale encoding for encode";
        mEncoding = QTextCodec::codecForLocale()->name();
    }

    kDebug() << "encoding: " << mEncoding;
    QTextCodec *codec = QTextCodec::codecForName(mEncoding.toAscii());

    kDebug() << "pushing added objects";
    res = pushObjects(encode(codec, mAdded), caldav_add_object, OK, RT);
    if (OK == res) {
#ifdef USE_CALDAV_MODIFY
        kDebug() << "pushing changed objects";
        res = pushObjects(encode(codec, mChanged), caldav_modify_object, OK, RT);
        if (OK == res) {
            kDebug() << "pushing deleted objects";
            res = pushObjects(encode(codec, mDeleted), caldav_delete_object, OK, RT);
        }
#else // if USE_CALDAV_MODIFY
        kDebug() << "pushing changed objects (delete)";
        res = pushObjects(encode(codec, mChanged), caldav_delete_object, OK, RT);
        if (OK == res) {
            kDebug() << "pushing changed objects (add)";
            res = pushObjects(encode(codec, mChanged), caldav_add_object, OK, RT);
            if (OK == res) {
                kDebug() << "pushing deleted objects";
                res = pushObjects(encode(codec, mDeleted), caldav_delete_object, OK, RT);
            }
        }
#endif // if USE_CALDAV_MODIFY
    }

    //if (OK != res) {
        clearObjects();
    //}

    return res;
}

// EOF ========================================================================
